package shop.controller.manage.offlineAccount;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.BaseController;
import shop.core.common.bean.ManageContainer;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.offlineAccount.OfflineAccountService;
import shop.services.manage.offlineAccount.bean.OfflineAccount;
import shop.services.manage.organize.OrganizeService;
import shop.services.manage.organize.bean.Organize;
import shop.services.manage.system.bean.User;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wzl on 2017/7/21.
 */
@Controller
@RequestMapping("/manage/offlineAccount/")
public class OfflineAccountAction extends BaseController<OfflineAccount> {
    private static final Logger logger = LoggerFactory.getLogger(OfflineAccountAction.class);
    @Autowired
    private OfflineAccountService offlineAccountService;
    @Autowired
    private OrganizeService organizeService;
    public OfflineAccountService getService() {
        return offlineAccountService;
    }


    /**
     * 查询爱心基金列表（机构账号）
     * @param request 请求
     * @param e       对象参数
     * @return
     */
    @Override
    @RequestMapping("loadData")
    @ResponseBody
    public PagerModel loadData(HttpServletRequest request, OfflineAccount e) {
        int offset = 0;
        int pageSize = 10;
        if (e.getOfflineName()!=null){
            e.setAccountName(e.getOfflineName());
        }
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if (pageSize < 0) {
            pageSize = 10;
        }
        e.setOffset(offset);
        e.setPageSize(pageSize);
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        Organize organize=new Organize();
        organize.setPrincipalPhone(user.getUsername());
        organize=organizeService.selectOne(organize);
        PagerModel pager = null;
        if(!organize.getStatus().equals("3")){
            pager = getService().selectPageList(e);
            pager.setRecordsTotal(pager.getTotal());
            pager.setRecordsFiltered(pager.getTotal());
        }
        return pager;
    }

    /**
     * 查询爱心基金列表（admin）
     * @param request 请求
     * @param e       对象参数
     * @return
     */
    @RequestMapping("loadAdminData")
    @ResponseBody
    public PagerModel loadAdminData(HttpServletRequest request, OfflineAccount e) {
        int offset = 0;
        int pageSize = 10;
        if (e.getOfflineName()!=null){
            e.setOrganizeName(e.getOfflineName());
        }
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if (pageSize < 0) {
            pageSize = 10;
        }
        e.setOffset(offset);
        e.setPageSize(pageSize);
        PagerModel pager = getService().selectAdminPageList(e);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

}
